package com.inspur.ourhis_afterend.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.ourhis_afterend.Dao.UserDao;
import com.inspur.ourhis_afterend.Entity.User;
import com.inspur.ourhis_afterend.Service.UserService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    // 根据用户名返回对象
    @Override
    public User findByUsername(String username) {
        System.out.println("username" + username);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userDao.selectOne(queryWrapper);
    }

    // 分页查询全部用户
    @Override
    public IPage<User> findAll(PageAction pageAction) {
        // 分页查询 接收的第一个参数为当前页数，第二个参数为页面的大小
        IPage<User> iPage = new Page<>(pageAction.getPageNo(), pageAction.getPageSize());
        return userDao.selectPage(iPage, null);
    }

    // 删除操作
    @Override
    public void del(Integer id) {
        // 按照id删除
        userDao.deleteById(id);
    }

    // 添加用户
    @Override
    public void add(User user) throws Exception {
        userDao.insert(user);
    }

    // 修改用户
    @Override
    public void edit(User user) throws Exception {
        userDao.updateById(user);
    }


    // 查询单个用户
    @Override
    public User findOne(Integer id) {
        return userDao.selectById(id);
    }

    // 通过用户名查询用户
    @Override
    public List<User> findByName(String realName) {
        System.out.println(realName);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("realName", realName);
        return userDao.selectList(queryWrapper);
    }

    @Override
    public List<User> findByRole(String role) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", role);
        return userDao.selectList(queryWrapper);
    }


}
